<template>
  <div class="article-page">
    <el-breadcrumb separator-class="el-icon-arrow-right">
      <el-breadcrumb-item>面经后台</el-breadcrumb-item>
      <el-breadcrumb-item>面经管理</el-breadcrumb-item>
    </el-breadcrumb>
    <el-card shadow="never" border="false">
      <template #header>
        <div class="header">
          <span>共 {{ total }} 条记录</span>
          <el-button
            icon="el-icon-plus"
            size="small"
            type="primary"
            round
            @click="openDrawer('add', null)"
          >
            添加面经
          </el-button>
        </div>
      </template>

      <!-- 表格部分 -->
      <template>
        <el-table :data="tableData" style="width: 100%">
          <el-table-column prop="stem" label="标题" width="300">
          </el-table-column>
          <el-table-column prop="creator" label="作者"> </el-table-column>
          <el-table-column prop="likeCount" label="点赞"> </el-table-column>
          <el-table-column prop="views" label="浏览数"> </el-table-column>
          <el-table-column prop="createdAt" label="更新时间"> </el-table-column>
          <el-table-column prop="" label="操作">
            <template #default="{ row }">
              <div class="actions">
                <i @click="openDrawer('view', row.id)" class="el-icon-view"></i>
                <i
                  @click="openDrawer('edit', row.id)"
                  class="el-icon-edit-outline"
                ></i>
                <i class="el-icon-delete" @click="del(row.id)"></i>
              </div>
            </template>
          </el-table-column>
        </el-table>
      </template>

      <el-pagination
        layout="prev, pager, next"
        :total="total"
        @current-change="pageChange"
      >
      </el-pagination>
    </el-card>

    <el-drawer
      :title="title"
      :visible.sync="isVisible"
      direction="rtl"
      :before-close="handleClose"
      size="50%"
    >
      <!-- 富文本编辑器 -->
      <div v-if="type === 'view'" class="article-preview">
        <h5>{{ form.stem }}</h5>
        <div v-html="form.content"></div>
      </div>

      <el-form v-else ref="form" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="标题" prop="stem">
          <el-input v-model="form.stem" placeholder="输入面经标题"></el-input>
        </el-form-item>
        <el-form-item label="内容" prop="content">
          <quill-editor
            v-model="form.content"
            @blur="$refs.form.validateField('content')"
          ></quill-editor>
        </el-form-item>
        <el-form-item>
          <el-button type="primary" @click="createArticle">确认</el-button>
          <el-button @click="handleClose">取消</el-button>
        </el-form-item>
      </el-form>
    </el-drawer>
  </div>
</template>

<script>
import 'quill/dist/quill.core.css'
import 'quill/dist/quill.snow.css'
import 'quill/dist/quill.bubble.css'
import { quillEditor } from 'vue-quill-editor'
import {
  getArticleList,
  createArticle,
  deleteArticle,
  getSingleDetail,
  updateArticle
} from '@/api/article'

export default {
  components: {
    quillEditor
  },
  name: 'ArticlePage',
  data () {
    return {
      tableData: [],
      current: 1,
      pageSize: 6,
      total: 0,
      type: '',
      isVisible: false,
      title: '',
      form: {
        stem: '',
        content: ''
      },
      rules: {
        // stem:{},
        // content:{}
        stem: [{ required: true, message: '请输入面经标题', trigger: 'blur' }],
        content: [
          { required: true, message: '请输入面经标题', trigger: 'blur' }
        ]
      }
    }
  },
  created () {
    this.getList()
  },
  methods: {
    async getList () {
      const { data: res } = await getArticleList({
        current: this.current,
        pageSize: this.pageSize
      })
      console.log(res)
      this.tableData = res.rows
      this.total = res.total
    },
    pageChange (val) {
      console.log(val)
      this.current = val
      this.getList()
    },
    async openDrawer (type, id) {
      this.type = type
      if (type === 'add') {
        this.isVisible = true
        this.title = '添加面经'
      }
      if (type === 'view') {
        this.isVisible = true
        this.title = '预览信息'
        console.log('这是id：' + id)
        const res = await getSingleDetail(id)
        console.log(res)
        this.form = {
          ...res.data
        }
      }
      if (type === 'edit') {
        this.isVisible = true
        this.title = '修改信息'
        const res = await getSingleDetail(id)
        console.log(res)
        this.form = {
          ...res.data
        }
      }
    },
    handleClose () {
      // if (this.type !== 'view') {
      //   this.$refs.form.resetFields()
      //   console.log(123456)
      // }
      this.form = {
        stem: '',
        content: ''
      }
      this.isVisible = false
    },
    async createArticle () {
      if (this.type === 'add') {
        const res = await createArticle(this.form)
        console.log(res)
        this.handleClose()
        this.current = 1
        this.$message.success('添加成功')
        this.getList()
      }
      if (this.type === 'edit') {
        const { id, stem, content } = this.form
        await updateArticle({ id, stem, content })
        this.$message.success('修改成功')
        this.getList()
        this.handleClose()
      }
    },
    async del (id) {
      await deleteArticle(id)
      this.$message.success('删除成功')
      this.getList()
    }
  },
  computed: {}
}
</script>

<style lang="scss" scoped>
.el-card {
  margin-top: 25px;
  .header {
    display: flex;
    justify-content: space-between;
    align-items: center;
    padding-right: 16px;
  }
  .actions {
    font-size: 18px;
    display: flex;
    justify-content: space-around;
    color: #666;
    > i:hover {
      color: rgba(114, 124, 245, 1);
      cursor: pointer;
    }
  }
}
.el-pagination {
  margin-top: 20px;
  text-align: center;
}
.el-breadcrumb {
  margin-top: 10px;
}
.el-form {
  padding-right: 40px;
}
.quill-editor {
  ::v-deep .ql-editor {
    height: 300px;
  }
}
.el-rate {
  padding: 10px 0;
}
.article-preview {
  padding: 0 40px 40px 40px;
  > h5 {
    font-size: 20px;
    color: #666;
    border-bottom: 1px dashed #ccc;
    padding-bottom: 30px;
    margin: 0 0 20px 0;
  }
}
</style>
